package com.tianshi.product.service.impl;



import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.tianshi.vo.ProductVO;
import com.tianshi.entity.Categories;
import com.tianshi.entity.Products;
import com.tianshi.product.mapper.CategoryMapper;
import com.tianshi.product.mapper.ProductMapper;
import com.tianshi.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public ProductVO getProductById(Long id) {
        Products productById = productMapper.getProductById(id);

        //categioryId
        Categories categoryById = categoryMapper.getCategoryById(productById.getCategoryId());

        ProductVO res = new ProductVO();
        res.setProduct(productById);
        res.setCategoryName(categoryById.getCategoryName());
        return res;
    }

    @SentinelResource(value = "getAllProducts", fallback = "getAllProductsFallback")
    @Override
    public List<ProductVO> getAllProducts() {
        List<Products> products = productMapper.getAllProducts();
        List<ProductVO> productDtos = new ArrayList<>();

        for (Products product : products) {
            ProductVO productDto = new ProductVO();

            Long categoryId = product.getCategoryId();

            Categories category = categoryMapper.getCategoryById(categoryId);
            if (category != null) {
                productDto.setCategoryName(category.getCategoryName());
            } else {
                productDto.setCategoryName("未知分类");
            }

            productDto.setProduct(product);
            productDtos.add(productDto);
        }

        return productDtos;
    }

//    public List<ProductVO> getAllProductsFallback(BlockException e) {
//        log.info("限流异常处理，异常信息为：{}", e.getMessage());
//        return new ArrayList<>();
//    }

    public List<ProductVO> searchProductByName(String productName) {
        List<Products> products = productMapper.searchProductByName(productName);

        List<ProductVO> productVOs = new ArrayList<>();
        for (Products product : products) {
            ProductVO vo = new ProductVO();
            vo.setProduct(product);

            // 处理分类名称
            Categories category = categoryMapper.getCategoryById(product.getCategoryId());
            vo.setCategoryName(category != null ? category.getCategoryName() : "未知分类");

            productVOs.add(vo);
        }
        return productVOs;
    }

    @Override
    public Long addProduct(Products product) {
        product.setCreatedAt(LocalDateTime.now()); //创建时间
        product.setUpdatedAt(LocalDateTime.now()); //更新时间
        return productMapper.addProduct(product);
    }

    @Override
    public Products updateProduct(Products product) {
        product.setUpdatedAt(LocalDateTime.now()); //更新时间
        Long updateProduct = productMapper.updateProduct(product);
        Products productById = productMapper.getProductById(updateProduct);
        return productById;
    }

    @Override
    public Long deleteProduct(Long productId) {
        Long deleteProduct = productMapper.deleteProduct(productId);
        return deleteProduct;
    }



    //根据关键字模糊查询商品
    @Override
    public List<Products> searchByKeyword(String keyword) {
        return productMapper.searchByKeyword(keyword);
    }

    @Override
    public List<Products> searchByCategory(Long categoryId) {
        return productMapper.searchByCategory(categoryId);
    }

    @Override
    public List<Products> getHotProducts() {
        return productMapper.getHotProducts(6);
    }


}
